package dk.au.cs.bdsi.mail.backend;

import dk.au.cs.bdsi.core.backend.AbstractListener;
import dk.au.cs.bdsi.core.backend.ListenerHandler;
import dk.au.cs.bdsi.core.backend.ListenerHandlerFactory;
import dk.au.cs.bdsi.mail.domain.Mail;
import dk.au.cs.bdsi.mail.domain.MailStatus;
import dk.au.cs.bdsi.mail.repositories.MailRepository;

/**
 * Listens for and processes all new {@link Mail} messages in the 
 * current {@link MailRepository}.
 */
public class MailListener extends AbstractListener<Mail> {
	public MailListener() {
		super(new HandlerFactory(), MailRepository.current());
	}
	
	protected static class HandlerFactory implements ListenerHandlerFactory<Mail> {
		@Override
		public ListenerHandler<Mail> create() {
			return new MailHandler();
		}
	}
	
	protected static class MailHandler implements ListenerHandler<Mail> {
		@Override
		public void handle(Mail message) {
			System.out.println("Trying to send " + message);
			// TODO Implement sending mechanism
			// For now, we will fail all messages
			message.setStatus(MailStatus.NotSent);
		}
	}
}